package com.spzx.channel.service.impl;

import com.spzx.channel.service.IndexService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.api.service.RemoteCategoryService;
import com.spzx.product.api.service.RemoteProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private RemoteCategoryService remoteCategoryService;

    @Autowired
    private RemoteProductService remoteProductService;

    @Override
    public Map<String, Object> getIndexData() {
        //远程调用商品微服务获取一级分类数据
        R<List<CategoryVo>> categoryListResult = remoteCategoryService.getOneCategory(SecurityConstants.INNER);
        //获取一级分类集合
        if (R.FAIL == categoryListResult.getCode()) {
            log.error(categoryListResult.getMsg());
        }
        //远程调用商品微服务获取畅销商品
        R<List<ProductSku>> productSkuListResult = remoteProductService.getTopSale(SecurityConstants.INNER);
        //获取畅销商品集合
        if (R.FAIL == productSkuListResult.getCode()) {
            log.error(productSkuListResult.getMsg());
        }
        //创建返回的Map
        Map<String, Object> Map = new HashMap<>();
        //设置一级分类数据
        Map.put("categoryList", categoryListResult.getData());
        //设置畅销商品信息
        Map.put("productSkuList", productSkuListResult.getData());
        return Map;
    }
}